package com.qk.common.event;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class EventPublisher {
    private static final Map<Class<?>, List<EventListener<?>>> listeners = new HashMap<>();
    
    public static <T> void subscribe(Class<T> eventType, EventListener<T> listener) {
        listeners.computeIfAbsent(eventType, k -> new ArrayList<>()).add(listener);
    }
    
    public static <T> void publish(T event) {
        Class<?> eventType = event.getClass();
        if (listeners.containsKey(eventType)) {
            for (EventListener<?> listener : listeners.get(eventType)) {
                ((EventListener<T>) listener).onEvent(event);
            }
        }
    }
}